const express = require('express');
const router = express.Router();
const db = require('../config/db');
const path = require('path');
router.get('/login', (req, res) => {
    res.sendFile(path.join(__dirname, '../public/login.html'));
});
router.get('/register', (req, res) => {
    res.sendFile(path.join(__dirname, '../public/register.html'));
});
router.get('/user-info', (req, res) => {
    if (req.session.user) {
        res.json(req.session.user);
    } else {
        res.json(null);
    }
});
router.post('/register', (req, res) => {
    const { username, password } = req.body;
    db.query('INSERT INTO users (username, password) VALUES (?, ?)', [username, password], (err, result) => {
        if (err) throw err;
        res.redirect('/auth/login');
    });
});
router.post('/login', (req, res) => {
    const { username, password } = req.body;
    db.query('SELECT * FROM users WHERE username = ? AND password = ?', [username, password], (err, results) => {
        if (err) throw err;
        if (results.length > 0) {
            req.session.user = results[0];
            res.redirect('/user');
        } else {
            res.redirect('/auth/login');
        }
    });
});

module.exports = router;